package com.topnav.mc.admin.entity.wrapper;


import com.topnav.mc.admin.entity.param.UserParam;
import com.topnav.mc.base.mc.McWrapper;
import org.springframework.util.StringUtils;

public class UserWrapper extends McWrapper<UserParam> {

    public UserWrapper(UserParam entity) {
        super(entity);
        if (this.permissionGrade != null ){
            if (this.permissionGrade.getPermissionLevel() == 1){
                this.eq("u.id",this.permissionGrade.getUserId());
            }else if (this.permissionGrade.getPermissionLevel() == 2){
                this.eq("u.org_id",this.permissionGrade.getOrgId());
            }else if (this.permissionGrade.getPermissionLevel() == 3 || this.permissionGrade.getPermissionLevel() == 4){
                this.likeRight("o.path",this.permissionGrade.getOrgPath());
            }
        }

        if (entity != null){
            if (entity.getId()!=null){this.eq("u.id",entity.getId());}
            if (!StringUtils.isEmpty(entity.getCode())){this.like("u.code",entity.getCode());}
            if (!StringUtils.isEmpty(entity.getName())){this.like("u.name",entity.getName());}
            if (!StringUtils.isEmpty(entity.getLoginName())){this.like("u.login_name",entity.getLoginName());}

            if (entity.getOrgId() != null){this.eq("u.org_id",entity.getOrgId());}
            if (entity.getEnabled()!=null){this.eq("u.enabled",entity.getEnabled());}

            if (!StringUtils.isEmpty(entity.getKeyword())){
                this.and(wrapper -> wrapper.like("u.code", entity.getKeyword())
                        .or().like("u.name", entity.getKeyword())
                        .or().like("u.login_name", entity.getKeyword())
                );
            }
            this.ne("u.code","admin");
            this.orderByDesc("u.id");

        }
    }
}
